<template>
  <div class="app-container">
    <div>
      <el-form
        :model="sestuForm"
        ref="sestuForm"
        v-show="showSearch"
        label-width="90px"
      >
        <el-form-item label="请选择考试" prop="radio">
          <!-- <div class="chose">
            <el-radio-group
              v-model="sestuForm.examinationId"
              @change="gedetail(sestuForm.examinationId)"
              style="margin: 10px 10px"
            >
              <el-radio-button
                v-for="(item, i) in examData"
                :key="i"
                :label="item.id"
                >{{ item.name }}</el-radio-button
              >
            </el-radio-group>
          </div> -->
          <div class="nav">
            <!-- <div
              v-for="(item, i) in examData"
              :key="i"
              :class="item.changecolor == false ? 'content' : 'contentchange'"
              @click="handleColor(item)"
            >
              {{ item }}
            </div> -->
            <el-select v-model="sestuForm.examinationId" filterable placeholder="请选择考试" @change="handleColor">
              <el-option
                v-for="item in examData"
                :key="item.value"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="请选择场次" prop="sessionId">
          <!-- <div class="chose">
            <span v-if="grouData == ''">请先选择考试</span>
            <el-radio-group
              v-model="sestuForm.sessionId"
              style="margin: 10px 10px"
              @change="getList()"
            >
              <el-radio-button
                v-for="(item, i) in grouData"
                :key="i"
                :label="item.id"
                >{{ item.name }}</el-radio-button
              >
            </el-radio-group>
          </div> -->
          <div class="nav">
            <div
              v-for="(item, i) in grouData"
              :key="i"
              :class="item.changecolor == false ? 'content' : 'contentchange'"
              @click="beforegetList(item.id)"
            >
              {{ item.name }}
            </div>
          </div>
        </el-form-item>
        <!-- <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item> -->
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >批量引用</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >批量删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考试表主键id" prop="examinationId">
        <el-input
          v-model="queryParams.examinationId"
          placeholder="请输入考试表主键id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考试表主键id" prop="paperId">
        <el-input
          v-model="queryParams.paperId"
          placeholder="请输入考试表主键id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label=" 状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入 状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考试场次id" prop="sessionId">
        <el-input
          v-model="queryParams.sessionId"
          placeholder="请输入考试场次id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['exam:examinationPaper:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['exam:examinationPaper:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['exam:examinationPaper:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['exam:examinationPaper:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="examinationPaperList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <!-- <el-table-column label="考试场次id" align="center" prop="id" />
      <el-table-column label="考试表主键id" align="center" prop="examinationId"/>
      <el-table-column label="考试表主键id" align="center" prop="paperId" />
      <el-table-column label=" 状态" align="center" prop="state" />
      <el-table-column label="考试场次id" align="center" prop="sessionId" /> -->

      <el-table-column label="试卷名称" align="center" prop="name" />
      <el-table-column label="答题时间(分钟)" align="center" prop="duration" />
      <el-table-column label="试卷平均难度" align="center" prop="difficulty" />
      <el-table-column label="试卷总分" align="center" prop="score" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['exam:examinationPaper:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleCheck(scope.row)"
            >预览</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam:examinationPaper:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="sestuForm.pageNum"
      :limit.sync="sestuForm.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改引入试卷对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="90%"
      append-to-body
      :before-close="handleClose"
    >
      <searchpaper :row="row" v-if="open"></searchpaper>
    </el-dialog>

    <!-- 预览整张试卷 -->

    <el-dialog width="80%" title :visible.sync="paper" v-if="paper">
      <showpaper :row="paperdata"></showpaper>
    </el-dialog>
  </div>
</template>

<script>
import {
  listExaminationPaper,
  getExaminationPaper,
  delExaminationPaper,
  addExaminationPaper,
  updateExaminationPaper,
  exportExaminationPaper,
} from '@/api/exam/examinationPaper'
import {
  listExam,
  addExam,
  getExam,
  delExame,
  updateExam,
} from '@/api/exam/set'
import searchpaper from './searchpaper.vue'
import showpaper from './showpaper.vue'

export default {
  name: 'ExaminationPaper',
  components: { searchpaper, showpaper },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      //展开层
      activeNamefirst: '1',
      activeNamesecond: '1',
      // 选中数组
      ids: [],
      //传入组件得值
      row: [],

      //是否显示试卷预览
      paper: false,
      //试卷预览的数据
      paperdata: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引入试卷表格数据
      examinationPaperList: [],
      // 弹出层标题
      title: '',
      //查询考生参数
      sestuForm: {
        examinationId: '',
        sessionId: '',
        pageNum: 1,
        pageSize: 10,
      },
      //数组
      listdata: [
        // {
        //   text: "请点一下",
        //   changecolor: false,
        // },
        // {
        //   text: "有些变化",
        //   changecolor: false,
        // },
        // {
        //   text: "什么变化",
        //   changecolor: false,
        // },
        // {
        //   text: "仔细看看",
        //   changecolor: false,
        // },
        // {
        //   text: "啊哈哈哈",
        //   changecolor: false,
        // },
      ],
      //试卷列表
      examData: [],
      //场地列表
      grouData: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examinationId: null,
        paperId: null,
        state: null,
        sessionId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.getListexam()
    console.log(this.sestuForm.examinationId);
  },
  methods: {
    handleColor(item) {
      // for (let i = 0; i < this.examData.length; i++) {
      //   this.examData[i].changecolor = false
      // }
      // for (let i = 0; i < this.grouData.length; i++) {
      //   this.grouData[i].changecolor = false
      // }
      // item.changecolor = !item.changecolor
      // this.sestuForm.examinationId = item.id
      this.gedetail(item)
    },
    /** 查询引入试卷列表 */
    getList() {
      this.loading = true
      listExaminationPaper(this.sestuForm).then((response) => {
        this.examinationPaperList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.getList()
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.reset()
          this.getList()
          done()
        })
        .catch((_) => {})
    },
    beforegetList(itemid) {
      for (let i = 0; i < this.grouData.length; i++) {
        if (this.grouData[i].id == itemid) {
          this.grouData[i].changecolor = true
        } else {
          this.grouData[i].changecolor = false
        }
      }
      this.sestuForm.sessionId = itemid
      this.getList()
    },
    //查询考试信息，场次
    gedetail(tempid) {
      const id = tempid
      getExam(id).then((response) => {
        this.grouData = response.data.sessionList
        //
        //给数组加判断条件
        for (let i = 0; i < this.grouData.length; i++) {
          if (i == 0) {
            this.grouData[i].changecolor = true
          } else {
            this.grouData[i].changecolor = false
          }
        }
        console.log(this.grouData)
        this.sestuForm.sessionId = this.grouData[0].id
        this.getList()
      })
    },

    //预览整张试卷
    handleCheck(row) {
      this.paper = true
      const ids = row.id || this.ids
      this.paperdata = {
        id: ids,
      }
      // getPaper(ids).then((response) => {
      //   this.paperdata=response.data
      //   console.log("试卷111",this.paperdata)
      // });
    },

    //获取考试列表
    getListexam() {
      this.loading = true
      listExam().then((response) => {
        this.examData = response.rows
        this.loading = false
        // 在这里加
        for (let i = 0; i < this.examData.length; i++) {
          if (i == 0) {
            this.examData[i].changecolor = true
          } else {
            this.examData[i].changecolor = false
          }
        }
        this.sestuForm.examinationId = this.examData[0].id
        //console.log(this.examData);

        this.gedetail(this.sestuForm.examinationId)
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        examinationId: null,
        createTime: null,
        paperId: null,
        updateBy: null,
        state: null,
        updateTime: null,
        sessionId: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.sessionPaperId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.row = this.sestuForm
      this.open = true
      this.title = '引入试卷'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getExaminationPaper(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改引入试卷'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateExaminationPaper(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addExaminationPaper(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.sessionPaperId || this.ids
      this.$confirm(
        '是否确认删除引入试卷编号为"' + ids + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(function () {
          return delExaminationPaper(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有引入试卷数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return exportExaminationPaper(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>
<style lang="scss" scoped>
.chose {
  background-color: #e4e7ed;
}
.nav {
  display: flex;
  flex-wrap: wrap;
}
.content,
.contentchange {
  display: flex;
  border-radius: 10px;
  margin-bottom: 10px;
  // margin-right: 10px;
  // right: 5px;
  // text-align: center;
  // min-height: 28px;
  // position: relative;
  // // display: block;
  padding: 8px 15px;
  // font: 16px/1.5 Arial, "微软雅黑", "Microsoft YaHei", Helvetica,
  //   "Helvetica Neue", Tahoma, Arial, sans-serif;
  // width: fit-content; //div宽度自适应文字内容
  // width: -webkit-fit-content;
  // width: -moz-fit-content;
}
.contentchange {
  margin-right: 10px;
  background: #409eff;
  color: #f1f1f1;
}
.content:hover {
  background: #f1f1f1;
}
</style>
